﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing.Printing;

namespace Tireguit.HelpPrint
{
    public class PrintChart
    {
        protected Chart _chart;
        private string PrintTitle;
        private string PrintFooter;
        private DateTime PrintDate;
        public PrintChart()
        {

        }
        public void Print(Chart chart,string title,string footer)
        {
            Print(chart, title, footer, DateTime.Now);
        }
        public void Print(Chart chart,string title,string footer,DateTime dateprint)
        {
            _chart = chart;
            PrintTitle = title;
            PrintFooter = footer;
            PrintDate = dateprint;
            if (_chart==null)
            {
                return;
            }
            DoPrint();
        }
        private void DoPrint()
        {
            PrintPreviewDialog ppvw;
            PrintDialog ppdialog;
            // Create new PrintDocument 
            System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
            // Add a PrintPageEventHandler for the document 
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            // Set the page orientation to landscape.
            pd.DefaultPageSettings.Landscape = true;
            ppdialog = new PrintDialog();
            if (ppdialog.ShowDialog()!=DialogResult.OK)
            {
                pd.PrintPage -= new PrintPageEventHandler(pd_PrintPage);
                return;
            }
            pd.DefaultPageSettings = ppdialog.PrinterSettings.DefaultPageSettings;
            ppvw = new PrintPreviewDialog();
            ppvw.Document = pd;
            if (ppvw.ShowDialog() != DialogResult.OK)
            {
                pd.PrintPage -= new PrintPageEventHandler(pd_PrintPage);
                return;
            }
            // Print the document 
            pd.Print();
            pd.PrintPage -= new PrintPageEventHandler(pd_PrintPage);

            /* Chart1.Printing.PageSetup();
             // Chart1.Printing.PrintPreview();
              Chart1.Printing.Print(true);*/
        }

        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            // Create and initialize print font 
            System.Drawing.Font printFont =
                new System.Drawing.Font("Arial", 10,FontStyle.Bold);
            // Create Rectangle structure, used to set the position of the chart Rectangle 
            //System.Drawing.Rectangle myRec = new System.Drawing.Rectangle(10, 30, 150, 150);
            // Draw Header
            SizeF measuretitle=ev.Graphics.MeasureString(PrintTitle, printFont, 
                ev.MarginBounds.Width);
            ev.Graphics.DrawString(PrintTitle, printFont, Brushes.Black,
                    ev.MarginBounds.Left, 
                    ev.MarginBounds.Top - measuretitle.Height - 13);

            String s = PrintDate.ToLongDateString() + " "
                + PrintDate.ToShortTimeString();

            ev.Graphics.DrawString(s, printFont,Brushes.Black, 
                ev.MarginBounds.Left + (ev.MarginBounds.Width - measuretitle.Width), 
                ev.MarginBounds.Top - measuretitle.Height - 13);
            System.Drawing.Rectangle myRec = 
                new System.Drawing.Rectangle(ev.MarginBounds.Left,
                    ev.MarginBounds.Top+(int)measuretitle.Height-13, 
                    ev.PageBounds.Width - ev.MarginBounds.Left,
                    ev.PageBounds.Height - ev.MarginBounds.Top -( 2*(int)measuretitle.Height) - 13-31);
            //Draw chart
            _chart.Printing.PrintPaint(ev.Graphics, myRec);
            //Draw Footer
            ev.Graphics.DrawString(PrintFooter, printFont, Brushes.Black,
                ev.MarginBounds.Left + (ev.MarginBounds.Width - measuretitle.Width) / 2,
                ev.MarginBounds.Top +   ev.MarginBounds.Height + 31);
        }
    }
}
